package tail.ui;

import javax.swing.event.DocumentEvent;
import tail.Tail;

/**
 * @author Michael Sena
 * @version 1.0
 */
public class Document extends javax.swing.JInternalFrame {
    /** Tail object. */
    private Tail tail = null;
    
    /** Inactive icon. */
    private static final javax.swing.ImageIcon INACTIVE_ICON =
        new javax.swing.ImageIcon(
        Document.class.getResource("/tail/ui/icons/media-record.png"));
    /** Active icon. */
    private static final javax.swing.ImageIcon ACTIVE_ICON =
        new javax.swing.ImageIcon(
        Document.class.getResource("/tail/ui/icons/media-record-green.png"));
    /**
     * Creates new form Document.
     * @param file Filename to tail
     */
    public Document(final String file) {
        initComponents();
        textArea.getDocument().addDocumentListener(new TailTextListner());
        setTitle(file);
        setFrameIcon(INACTIVE_ICON);
        tail = new Tail(file, textArea);
        startTail();
    }

    /**
     * Starts the thread process.
     */
    private void startTail() {
        tail.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("[loading]");
        setAutoscrolls(true);
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/tail/ui/icons/media-record.png"))); // NOI18N
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                clickHandler(evt);
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                focusHandler(evt);
            }
        });

        textArea.setColumns(20);
        textArea.setEditable(false);
        textArea.setRows(5);
        textArea.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                clickedHandler(evt);
            }
        });
        jScrollPane1.setViewportView(textArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 486, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clickHandler(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clickHandler
        System.out.println("ASDFASDF 2");
    }//GEN-LAST:event_clickHandler

    private void focusHandler(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_focusHandler
        System.out.println("ASDFASDF 3");
    }//GEN-LAST:event_focusHandler

    private void clickedHandler(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clickedHandler
        System.out.println("ASDFASDF 4");
        setFrameIcon(INACTIVE_ICON);
    }//GEN-LAST:event_clickedHandler
    
    /**
     * Implementation of document listener.
     */
    class TailTextListner implements javax.swing.event.DocumentListener {
        /** Scrolls to the end of the text area. */
        private void scrollToEnd() {
            textArea.setCaretPosition(textArea.getDocument().getLength());
            if(hasFocus()) {
                System.out.println("HERE");
                setFrameIcon(ACTIVE_ICON);
            }
        }
        @Override
        public void insertUpdate(final DocumentEvent e) {
            scrollToEnd();
        }

        /**
         * Unused.
         * @param e Document event raised
         */
        @Override
        public void removeUpdate(final DocumentEvent e) { }

        /**
         * Unused.
         * @param e Document event raised
         */
        @Override
        public void changedUpdate(final DocumentEvent e) {
            
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables

}
